<template>
  <div class="dialog-wrapper">
    <el-dialog title="填写物流信息：" :visible.sync="dialogVisible" width="30%" :before-close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="物流类型:" prop="logistics">
          <el-select v-model="form.logistics" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="物流单号:" prop="logistics_number">
          <el-input v-model="form.logistics_number" size="mini" style="width: 300px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getDeliverList, setSeckillDeliver } from '@/api/seckill'
const ERR_OK = 200
export default {
  props: {
    remark: {
      type: String,
      default: ''
    },
    id: {
      type: [String, Number],
      default: ''
    },
    status: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      form: {
        id: '',
        logistics: '',
        logistics_number: ''
      },
      disabled: true,
      dialogVisible: false,
      options: [],
      value: '',
      rules: {
        logistics: [
          { required: true, message: '请选择物流', trigger: 'blur' }
        ],
        logistics_number: [
          { required: true, message: '请输入物流单号', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    remark(newVal) {
      // this.form.remark = newVal
    },
    id(newVal) {
      this.form.id = newVal
    }
  },
  created() {
    this._getDeliverList()
  },
  methods: {
    show() {
      this.dialogVisible = true
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this._setSeckillDeliver(this.form)
            .then(() => {
              this.$emit('success')
              this.dialogVisible = false
            })
        } else {
          return false
        }
      })
    },
    cancel() {
      this.dialogVisible = false
    },
    _getDeliverList() {
      getDeliverList()
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res, 'getDeliverList')
            this.options = res.data
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _setSeckillDeliver({ id, logistics, logistics_number }) {
      return setSeckillDeliver({ id, logistics, logistics_number })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.$message({
              message: res.message,
              type: 'success'
            })
            return Promise.resolve()
          }
        })
        .catch(err => {
          console.log(err)
          return Promise.reject()
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/el-button.scss";
.el-form /deep/ .el-form-item__label {
  white-space: nowrap;
  color: rgba(102, 102, 102, 1);
  font-size: 14px;
  font-family: Roboto;
}
.el-form /deep/ .el-input__inner {
  width: 278px;
  height: 36px;
}
.el-form /deep/ .el-textarea__inner {
  width: 278px;
  height: 137px;
}
.dialog-footer {
  button{
    &:nth-child(1){
      background-color: rgba(255, 255, 255, 1);
      color: rgba(16, 16, 16, 1);
      font-size: 14px;
      text-align: center;
      font-family: Roboto;
    }
  }
}
.dialog-wrapper{
  .el-dialog__wrapper /deep/ .el-dialog{
    min-width: 400px !important;
  }
}
</style>
